package com.invengo.library_rfid.analyze.utils.uhf.formatHelp;

public class CBitClass implements Comparable {
    /// 当前写入的字节长度
    public int writed_byte_length() {
        return m_nByteLength;
    }
    /// 比特流
    public byte[] bytes_value() {
        return m_byValue;
    }
    /// 长度(以byte为单位)
    private int m_nByteLength;
    /// 空余bit位
    private int m_nEmptyBit;
    /// 值
    public byte[] m_byValue;
    /// 名称：数据流预计长度
    public int m_nLength = 12;
    public CBitClass() {
        m_nLength = 12;
        m_nByteLength = 0;
        m_nEmptyBit = 0;
        m_byValue = new byte[12];
    }
    /// 用于接收借书卡
    /// 修改：吴永祥，2011年3月8日，去掉m_nByteLength的初始值 nLength
    /// <param name="nLength"></param>
    public CBitClass(int nLength) {
        m_nLength = nLength;
        m_nByteLength = 0;
        m_nEmptyBit = 0;
        m_byValue = new byte[nLength];
    }
    public CBitClass(byte[] byData) {
        m_nLength = byData.length;
        m_nByteLength = byData.length;
        m_nEmptyBit = 0;
        m_byValue = new byte[byData.length];
        System.arraycopy(byData, 0, m_byValue, 0, byData.length);
    }
    /// 名称：添加字符串，每一个字母为一个字节
    /// <param name="szValue">字符串</param>
    public int AddBits(String szValue) {
        for (int i = 0; i < szValue.length(); i++) {
            m_byValue[m_nByteLength] += (byte)szValue.charAt(i);
            m_nByteLength++;
        }
        return m_nByteLength;
    }
    /// 名称：添加字符串（支持中文汉字），每一个汉字为两个字节
    /// <param name="szValue">字符串</param>
    public int AddBitsWithChinese(String szValue) {
        for (int i = 0; i < szValue.length(); i++) {
            byte[] tempBytes = szValue.substring(i, i + 1).getBytes();
            for (byte tempByte : tempBytes) {
                m_byValue[m_nByteLength] += tempByte;
                m_nByteLength++;
            }
        }
        return m_nByteLength;
    }
    /// 作用    : 添加bit数据
    /// <param name="nVal">插入数据的值</param>
    /// <param name="nBitLength">插入数据的bit长度</param>
    /// 插入数据后,m_byValue的长度(以byte为单位)
    public int AddBits(long lVal, int nBitLength) {
        //Debug.Assert(nBitLength <= 64);
        //Debug.Assert((m_nByteLength + (nBitLength + m_nEmptyBit) / 8) <= m_nLength);
        int nUsed = 0;
        try {
            if (m_nEmptyBit != 0) {
                //空余位数 > nBitLength
                if (m_nEmptyBit >= nBitLength) {
                    /*
                     * 1.将nVal中的nBitLength有效位补齐到m_nEmptyBit位; (lVal << (m_nEmtpyBit - nBitLength));
                     * 2.插入到空余位中;
                     * 3.标志空余位数, m_nByteLength;  m_nEmptyBit -= nBitLength;
                     * 4.返回
                    */
                    m_byValue[m_nByteLength] += (byte)(lVal << (m_nEmptyBit - nBitLength));
                    if (m_nEmptyBit == nBitLength) {
                        ++m_nByteLength;
                    }
                    m_nEmptyBit -= nBitLength;
                    return m_nByteLength;
                } else {
                    /*
                     * 1.从nVal中取出m_nEmptyBit位; lVal >> (nBitLength - m_nEmpty);
                     * 2.插入到空余位中;
                     * 3.标志nUsed, m_nEmtpyBit;    nUsed = m_nEmptyBit, m_nEmtpyBit = 0;
                    */
                    m_byValue[m_nByteLength] += (byte)(lVal >> (nBitLength - m_nEmptyBit));
                    nUsed = m_nEmptyBit;
                    m_nEmptyBit = 0;
                    ++m_nByteLength;
                }
            }
            //	repeat依次插入8位,知道不足8位
            while ((nBitLength - nUsed) >= 8) {
                /*
                 * 1.从剩余位数中取出8为;
                 *      a.取出剩余的位数;   (lVal << (64 - (nBitLength - nUsed)));
                 *      b.截取8位;          >> 56;
                 * 2.插入到m_byValue中;
                 * 3.标志nUsed, m_nByteLength;             nUsed += 8;
                */
                m_byValue[m_nByteLength] += (byte)((lVal << (64 - (nBitLength - nUsed))) >> 56);
                nUsed += 8;
                ++m_nByteLength;
            }
            //	将剩余不足8位的数据插入到m_byValue中;
            if (nUsed != nBitLength) {
                /*
                 * 1.取出剩余的位数;        (lVal << (64 - (nBitLength - nUsed)));
                 * 2.补齐8位;               >> 56;
                 * 3.插入到m_byValue中;
                 * 4.标志m_nEmptyBit;       m_nEmptyBit = 8 - (nBitLength - nUsed);
                */
                m_byValue[m_nByteLength] = (byte)((lVal << (64 - (nBitLength - nUsed))) >> 56);
                m_nEmptyBit = 8 - (nBitLength - nUsed);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return m_nByteLength;
    }
    /// 功能：添加字节流
    public int AddBytes(byte[] bytes) {
        if (m_nEmptyBit == 0) {
            for (int i = 0; i < bytes.length; i++) {
                m_byValue[m_nByteLength] += bytes[i];
                m_nByteLength++;
                if (m_nByteLength == m_nLength) { break; }
            }
            return m_nByteLength;
        } else {
            for (int i = 0; i < bytes.length; i++) {
                m_byValue[m_nByteLength] += (byte)(bytes[i] >> (8 - m_nEmptyBit));
                m_nByteLength++;
                if (m_nByteLength == m_nLength) { break; }
                m_byValue[m_nByteLength] += (byte)(bytes[i] << m_nEmptyBit);
            }
            return m_nByteLength;
        }
    }
    /// 作用    : 添加bit数据
    /// <param name="ulVal">插入数据的值</param>
    /// <param name="nBitLength">插入数据的bit长度</param>
    /// 插入数据后,m_byValue的长度(以byte为单位)
//    public int AddBits(long ulVal, int nBitLength) {
//        //Debug.Assert(nBitLength <= 64);
//        //Debug.Assert((m_nByteLength + (nBitLength + m_nEmptyBit) / 8) <= m_nLength);
//        int nUsed = 0;
//        try {
//            if (m_nEmptyBit != 0) {
//                //空余位数 > nBitLength
//                if (m_nEmptyBit >= nBitLength) {
//                    /*
//                     * 1.将nVal中的nBitLength有效位补齐到m_nEmptyBit位; (lVal << (m_nEmtpyBit - nBitLength));
//                     * 2.插入到空余位中;
//                     * 3.标志空余位数, m_nByteLength;  m_nEmptyBit -= nBitLength;
//                     * 4.返回
//                    */
//                    m_byValue[m_nByteLength] += (byte)(ulVal << (m_nEmptyBit - nBitLength));
//                    if (m_nEmptyBit == nBitLength) {
//                        ++m_nByteLength;
//                    }
//                    m_nEmptyBit -= nBitLength;
//                    return m_nByteLength;
//                } else {
//                    /*
//                     * 1.从nVal中取出m_nEmptyBit位; lVal >> (nBitLength - m_nEmpty);
//                     * 2.插入到空余位中;
//                     * 3.标志nUsed, m_nEmtpyBit;    nUsed = m_nEmptyBit, m_nEmtpyBit = 0;
//                    */
//                    m_byValue[m_nByteLength] += (byte)(ulVal >> (nBitLength - m_nEmptyBit));
//                    nUsed = m_nEmptyBit;
//                    m_nEmptyBit = 0;
//                    ++m_nByteLength;
//                }
//            }
//            //	repeat依次插入8位,知道不足8位
//            while ((nBitLength - nUsed) >= 8) {
//                /*
//                 * 1.从剩余位数中取出8为;
//                 *      a.取出剩余的位数;   (lVal << (64 - (nBitLength - nUsed)));
//                 *      b.截取8位;          >> 56;
//                 * 2.插入到m_byValue中;
//                 * 3.标志nUsed, m_nByteLength;             nUsed += 8;
//                */
//                m_byValue[m_nByteLength] += (byte)((ulVal << (64 - (nBitLength - nUsed))) >> 56);
//                nUsed += 8;
//                ++m_nByteLength;
//            }
//            //	将剩余不足8位的数据插入到m_byValue中;
//            if (nUsed != nBitLength) {
//                /*
//                 * 1.取出剩余的位数;        (lVal << (64 - (nBitLength - nUsed)));
//                 * 2.补齐8位;               >> 56;
//                 * 3.插入到m_byValue中;
//                 * 4.标志m_nEmptyBit;       m_nEmptyBit = 8 - (nBitLength - nUsed);
//                */
//                m_byValue[m_nByteLength] = (byte)((ulVal << (64 - (nBitLength - nUsed))) >> 56);
//                m_nEmptyBit = 8 - (nBitLength - nUsed);
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
////            Logger.Error("添加bit数据失败：" + ex.getMessage() + " " + ex.getStackTrace());
//        }
//        return m_nByteLength;
//    }
    /// 作用    : 添加bit数据
    /// 作者    : 叶德摇
    /// <param name="nVal">插入数据的值</param>
    /// <param name="nBitLength">插入数据的bit长度</param>
    /// 插入数据后,m_byValue的长度(以byte为单位)
    public int AddBits(long lVal, int nBitLength, int nSumLength) {
        //Debug.Assert(nBitLength <= 64);
        //Debug.Assert((m_nByteLength + (nBitLength + m_nEmptyBit) / 8) <= nSumLength);
        int nUsed = 0;
        try {
            if (m_nEmptyBit != 0) {
                //空余位数 > nBitLength
                if (m_nEmptyBit >= nBitLength) {
                    /*
                     * 1.将nVal中的nBitLength有效位补齐到m_nEmptyBit位; (lVal << (m_nEmtpyBit - nBitLength));
                     * 2.插入到空余位中;
                     * 3.标志空余位数, m_nByteLength;  m_nEmptyBit -= nBitLength;
                     * 4.返回
                    */
                    m_byValue[m_nByteLength] += (byte)(lVal << (m_nEmptyBit - nBitLength));
                    if (m_nEmptyBit == nBitLength) {
                        ++m_nByteLength;
                    }
                    m_nEmptyBit -= nBitLength;
                    return m_nByteLength;
                } else {
                    /*
                     * 1.从nVal中取出m_nEmptyBit位; lVal >> (nBitLength - m_nEmpty);
                     * 2.插入到空余位中;
                     * 3.标志nUsed, m_nEmtpyBit;    nUsed = m_nEmptyBit, m_nEmtpyBit = 0;
                    */
                    m_byValue[m_nByteLength] += (byte)(lVal >> (nBitLength - m_nEmptyBit));
                    nUsed = m_nEmptyBit;
                    m_nEmptyBit = 0;
                    ++m_nByteLength;
                }
            }
            //	repeat依次插入8位,知道不足8位
            while ((nBitLength - nUsed) >= 8) {
                /*
                 * 1.从剩余位数中取出8为;
                 *      a.取出剩余的位数;   (lVal << (64 - (nBitLength - nUsed)));
                 *      b.截取8位;          >> 56;
                 * 2.插入到m_byValue中;
                 * 3.标志nUsed, m_nByteLength;             nUsed += 8;
                */
                m_byValue[m_nByteLength] += (byte)((lVal << (64 - (nBitLength - nUsed))) >> 56);
                nUsed += 8;
                ++m_nByteLength;
            }
            //	将剩余不足8位的数据插入到m_byValue中;
            if (nUsed != nBitLength) {
                /*
                 * 1.取出剩余的位数;        (lVal << (64 - (nBitLength - nUsed)));
                 * 2.补齐8位;               >> 56;
                 * 3.插入到m_byValue中;
                 * 4.标志m_nEmptyBit;       m_nEmptyBit = 8 - (nBitLength - nUsed);
                */
                m_byValue[m_nByteLength] = (byte)((lVal << (64 - (nBitLength - nUsed))) >> 56);
                m_nEmptyBit = 8 - (nBitLength - nUsed);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
//            Logger.Error("添加bit数据失败：" + ex.getMessage() + " " + ex.getStackTrace());
        }
        return m_nByteLength;
    }
    /// 作用    : 获取指定区间内bit的值
    /// <param name="nStartBitPos">起始bit位</param>
    /// <param name="nBitLength">bit长度</param>
    /// 指定区间内bit组成的值
    public long GetValue(int nStartBitPos, int nBitLength) {
        //Debug.Assert((nStartBitPos + nBitLength) <= (m_nByteLength * 8 + m_nEmptyBit));
        //Debug.Assert(nBitLength <= 64);
        //是否在前缀有bit
        int nGet = 0;
        int nPreRemain = (nStartBitPos + 7 - (nStartBitPos + 7) % 8 - 1) - nStartBitPos + 1;
        int nPos = nStartBitPos / 8;
        long nResult = 0;
        try {
            if (0 != nPreRemain) {
                //                if (nBitLength <= (8 - nPreRemain))
                if (nBitLength <= nPreRemain) {
                    nGet = nBitLength;
                    byte bStep1 = (byte)(m_byValue[nPos] << (8 - nPreRemain));
                    byte bStep2 = (byte)(bStep1 >> (8 - nBitLength));
                    nResult = (long)bStep2& 0xFF;
                    return nResult;
                } else {
                    nGet = nPreRemain;
                    byte bStep1 = (byte)(m_byValue[nPos] << (8 - nPreRemain));
                    byte bStep2 = (byte)(bStep1 >> (8 - nPreRemain));
                    nResult = ((long)bStep2) << (nBitLength - nPreRemain);
                }
                ++nPos;
            }
            while (nBitLength - nGet >= 8) {
                nResult += (long)((long)(m_byValue[nPos]& 0xFF) << (nBitLength - nGet - 8));
                nGet += 8;
                ++nPos;
            }
            //是否还存在后缀bit
            if (nBitLength != nGet) {
                nResult += (long)((long)(m_byValue[nPos]) >> (8 - (nBitLength - nGet)));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
//            Logger.Error("获取指定区间内bit的值失败：" + ex.getMessage() + " " + ex.getStackTrace());
        }
        return nResult;
    }
    /// 作用    : 获取指定区间内bit的值
    /// <param name="nStartBitPos">起始bit位</param>
    /// <param name="nBitLength">bit长度</param>
    /// 指定区间内bit组成的值
    public long GetValueUL(int nStartBitPos, int nBitLength) {
        //Debug.Assert((nStartBitPos + nBitLength) <= (m_nByteLength * 8 + m_nEmptyBit));
        //Debug.Assert(nBitLength <= 64);
        //是否在前缀有bit
        int nGet = 0;
        int nPreRemain = (nStartBitPos + 7 - (nStartBitPos + 7) % 8 - 1) - nStartBitPos + 1;
        int nPos = nStartBitPos / 8;
        long nResult = 0;
        try {
            if (0 != nPreRemain) {
                //                if (nBitLength <= (8 - nPreRemain))
                if (nBitLength <= nPreRemain) {
                    nGet = nBitLength;
                    byte bStep1 = (byte)(m_byValue[nPos] << (8 - nPreRemain));
                    byte bStep2 = (byte)(bStep1 >> (8 - nBitLength));
                    nResult = (long)bStep2;
                    return nResult;
                } else {
                    nGet = nPreRemain;
                    byte bStep1 = (byte)(m_byValue[nPos] << (8 - nPreRemain));
                    byte bStep2 = (byte)(bStep1 >> (8 - nPreRemain));
                    nResult = ((long)bStep2) << (nBitLength - nPreRemain);
                }
                ++nPos;
            }
            while (nBitLength - nGet >= 8) {
                nResult += (long)((long)(m_byValue[nPos]) << (nBitLength - nGet - 8));
                nGet += 8;
                ++nPos;
            }
            //是否还存在后缀bit
            if (nBitLength != nGet) {
                nResult += (long)((long)(m_byValue[nPos]) >> (8 - (nBitLength - nGet)));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
//            Logger.Error("获取指定区间内bit的值失败：" + ex.getMessage() + " " + ex.getStackTrace());
        }
        return nResult;
    }
    /// 作用    : 获取byte数组
    /// 获取byte数组的缓存
    public int GetBytes(byte[] byArray) {
        System.arraycopy(m_byValue, 0, byArray, 0, m_nByteLength);
        return m_nByteLength;
    }
    /// 是否有空余的bit位
    /// false   : 无空余bit位
    /// true    : 有空余bit位
    public boolean HasEmpty() {
        if (0 != m_nEmptyBit) {
            return true;
        }
        return false;
    }
    /// 作用    : 以字符串显示CBitClass
    public String toString() {
        String szTemp = "";
        for (int i = 0; i < m_nByteLength; ++i) {
            szTemp += (String.format("%02X", m_byValue[i]));
        }
        if (m_nEmptyBit != 0) {
            szTemp += (String.format("%02X", m_byValue[m_nByteLength]));
        }
        return szTemp;
    }
    public int compareTo(Object obj) {
        if ((obj.getClass() != getClass()) || null == obj) {
            return -1;
        }
        CBitClass other = (CBitClass)obj;
        return toString().compareTo(obj.toString());
    }
}


